using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GP2D3D_Huiswerk2_RickBeijer_S1012261
{
    public class Explosion : DrawableGameComponent
    {
        private static List<KeyValuePair<Vector2, Rectangle>> frames;
        private int currentFrame;
        private int updates;

        public Point Location
        {
            get;
            set;
        }

        public Texture2D Image
        {
            get;
            set;
        }

        public Rectangle CurrentFrame
        {
            get { return frames[currentFrame].Value; }
        }

        public Vector2 CurrentOrigin
        {
            get { return frames[currentFrame].Key; }
        }

        public Explosion(Game game, Point location, Texture2D image)
            : base(game)
        {
            this.currentFrame = 0;
            this.Location = location;
            this.Image = image;

            if (frames == null)
            {
                frames = new List<KeyValuePair<Vector2, Rectangle>>();
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(5, 5), new Rectangle(16, 15, 10, 9)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(7, 10), new Rectangle(41, 10, 24, 19)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(10, 13), new Rectangle(75, 6, 31, 25)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(10, 16), new Rectangle(120, 3, 30, 31)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(10, 16), new Rectangle(168, 2, 30, 31)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(10, 16), new Rectangle(220, 2, 31, 31)));
                frames.Add(new KeyValuePair<Vector2, Rectangle>(new Vector2(10, 17), new Rectangle(277, 0, 32, 33)));
            }
        }

        public override void Update(GameTime gameTime)
        {
            // Don't change the image every update.
            updates++;

            if (updates > 100)
            {
                updates = 0;
            }

            // Keep animating, but not every update
            if (updates % 5 == 0)
            {
                currentFrame++;
            }

            // If we have not yet reached the max frames for this explosion, just keep animating
            if (currentFrame >= 7)
            {
                // Let the ProjectileList know that this explosion has died out
                ( (IProjectileList) Game.Services.GetService( typeof(IProjectileList) ) ).RemoveExplosion( this );
            }

            base.Update(gameTime);
        }
    }
}